<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-view"
          size="mini"
          @click="handleAllRead"
        >
          全部已读
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="announcementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="10" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip />
      <el-table-column label="发布人" align="center" prop="sender" />
      <el-table-column label="优先级" align="center" prop="priorityDesc" />
      <el-table-column label="消息类型" align="center" prop="msgCategoryDesc" />
      <el-table-column label="发布时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="阅读状态" align="center" prop="readFlag">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.readFlag" type="success">
            已读
          </el-tag>
          <el-tag v-else type="danger">
            未读
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog v-if="showOpen" title="通知详情" :visible.sync="showOpen" width="800px" append-to-body>
      <iframe :src="showDetailUrl" class="detail-iframe" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="showOpen=false">
          取 消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMyAnnouncementSend,
  readAnnouncementSend,
  readAllAnnouncementSend
} from "@/api/system/announcement.js"
import {getTokenUrl} from "../../../utils/auth"

export default {
  name: "MyMessage",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统通告表格数据
      announcementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showOpen: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        delFlag: false,
        title: null
      },
      showDetailUrl: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询系统通告列表 */
    getList() {
      this.loading = true
      getMyAnnouncementSend(this.queryParams).then((response) => {
        this.announcementList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleView(row) {
      if (!row.readFlag) {
        readAnnouncementSend(row.anntId).then(() => {
        })
      }
      // 打开方式
      if (row.openType && row.openType === "url") {
        this.$router.push({
          path: row.openPage
        })
      } else {
        this.showOpen = true
        this.showDetailUrl = getTokenUrl('/sys/announce/show/' + row.anntId)
      }
    },
    handleAllRead() {
      this.$confirm('是否确认标记所有的消息为已读"？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return readAllAnnouncementSend()
      }).then(() => {
        this.getList()
      }).catch(() => {
      })
    }
  }
}
</script>
<style>
.detail-iframe {
  border: 0;
  width: 100%;
  min-height: 600px;
}
</style>
